package com.neusoft.zygh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.neusoft.zygh.model.Hospitals;
import com.neusoft.zygh.service.HospitalsService;
import com.neusoft.zygh.utils.PageBean;
import com.neusoft.zygh.utils.Result;
import com.neusoft.zygh.utils.ResultGenerator;  


@RestController
@RequestMapping("/hospitals")
public class HospitalsController {
    @Autowired
    private HospitalsService hospitalsService;

    /**
     * 新增
     */
    @PostMapping("/insert")
    public Result<String> insert(@RequestBody Hospitals hospitals){
        int res=hospitalsService.insert(hospitals);
        if(res==1){
            return ResultGenerator.genSuccessResult();
        }else{
        	return ResultGenerator.genFailResult(null);
        }
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{ids}")
    public Result<String> delete(@PathVariable("ids") String ids){
    	if(null==ids||ids.equals("")){
    		return ResultGenerator.genFailResult(null);
    	}
        String str[]=ids.split(",");
        for(String s:str){
            hospitalsService.deleteByPrimaryKey(Integer.valueOf(s));
        }
        return ResultGenerator.genSuccessResult();
    }
    
    /**
     * 更新
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody Hospitals hospitals){
    	int res=hospitalsService.updateByPrimaryKey(hospitals);
    	if(res==1){
    		return ResultGenerator.genSuccessResult();
    	}else{
    		return ResultGenerator.genFailResult(null);
    	}
    }
    
    /**
     * 主键查询
     */
    @GetMapping("/info/{id}")
    public Result<?> selectById(@PathVariable("id") int id){
    	Hospitals hospitals = hospitalsService.selectByPrimaryKey(id);
    	if(null==hospitals){
    		return ResultGenerator.genFailResult(null);
    	}else{
    		return ResultGenerator.genSuccessResult(hospitals);
    	}
    }
    
    /**
     * 条件和排序的分页查询
     */
    @PostMapping("/search")
    public Map<String,Object> search(
    		@RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            Hospitals hospitals){
    	Map<String, Object> map = new HashMap<String, Object>();
        if (page != null && rows != null) {
            PageBean pageBean = new PageBean(Integer.parseInt(page),
                    Integer.parseInt(rows));
            map.put("start", pageBean.getStart());
            map.put("size", pageBean.getPageSize());
        }
        
        List<Hospitals> list=hospitalsService.select(map);
        int total=hospitalsService.count(map);
        Map<String,Object> result=new HashMap<>();
        result.put("rows", list);
        result.put("total", total);
        return result;
    }
    
    /**
     * 按地区条件筛选
     */
    @ResponseBody
    @RequestMapping(value="/selectByMore",method=RequestMethod.GET)
    public Map<String,Object> selectByMore(String province){
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("province", province);
    	
        List<Hospitals> list=hospitalsService.select(map);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("rows", list);
        return resultMap;
    }
}